package com.hl.controller;

import com.hl.domain.Article;
import com.hl.service.ArticleService;
import com.hl.util.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author Hr
 * @date 2024/9/5 20:49
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //查询所有
    @GetMapping("/findAllArticle")
    public R findAllArticle(Integer pageNum,Integer pageSize){
        return articleService.findAllArticle(pageNum, pageSize) != null ? R.success(articleService.findAllArticle(pageNum, pageSize)) : R.fail(articleService.findAllArticle(pageNum, pageSize));
    }

    //文件上传
    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        String path = "D:\\picture\\";

        String originalFilename = file.getOriginalFilename();
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + suffixName;
        file.transferTo(new File(path + newFileName));
        return R.success(newFileName);
    }

    //文件下载
    @GetMapping("/download")
    public void download(String filename, HttpServletResponse response) throws IOException {
        String path = "D:\\picture\\" + filename;

        //Files.newInputStream(Paths.get(path))
        InputStream inputStream = new FileInputStream(path);

        ServletOutputStream outputStream = response.getOutputStream();

        byte[] bytes = new byte[1024];
        int len = inputStream.read(bytes);
        while(len != -1){
            outputStream.write(bytes,0,len);
            len = inputStream.read(bytes);
        }
        outputStream.close();
        inputStream.close();
    }

    //新增
    @PostMapping("/addArticle")
    public R insertSelective(Article record){
        return  R.success(articleService.insertSelective(record));
    }

    //修改
    @PutMapping("/update")
    public R updateByPrimaryKeySelective(@RequestBody Article record){
//        System.out.println(record.getTeacherId());
        int row = articleService.updateByPrimaryKeySelective(record);
        return R.success(row);
    }

    //删除
    @DeleteMapping("/delete")
    public R deleteByPrimaryKey(Long id){
        int row = articleService.deleteByPrimaryKey(id);
        return R.success(row);
    }

    //条件查询
    @GetMapping("/findAllByLike")
    public R findAllByLike(String articlename, String typeName, @DateTimeFormat(pattern = "yyyy-MM-dd")
    Date startTime, @DateTimeFormat(pattern = "yyyy-MM-dd")Date endTime, Integer pageNum, Integer pageSize){
        System.out.println(articlename);
        System.out.println(startTime);
        System.out.println(endTime);
        Map<String, Article> allByLike = articleService.findAllByLike(articlename, typeName, startTime, endTime, pageNum, pageSize);
        return allByLike != null ? R.success(allByLike) : R.fail(allByLike);
    }

}
